/*Trade Dialog
 * what can be traded? -- properties without houses/hotel, railroad, util, adding cash.
 * Even though they're mortgaged, can be traded.
 * 
 * TradeDialog -- panelOnTradeDialog(JPanel) -- propPanel(JPanel) -- two scrollPane(tPane, bPane)showing props, two labels. 
 * 											 -- buttomPanel -- has two buttons, request and cancel
 * 											 -- moneyPanel -- showing player name and cash on east
 * 
 * We cannot trade when
 * -- the player who is requested has no item to be traded(no item or every prop group has some building AND no railroad AND no util.
 * -- user tries to exchange only cash.(the player has to select at least one of the properties) 
 * -- entered cash exceeds actual amount of cash.
 */
import javax.swing.*;

import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class TradeDialog extends JDialog implements ActionListener{

	Player cp, op;
	JPanel panelOnTradeDialog;//main Panel placed on the dialog.
	JPanel propPanel;//set to boxlayout. 
	JButton request, cancel;
	static int cashOfOpponent;
	static int cashOfPlayer;
	static int playerTotalGain;
	static int opTotalGain;
	JLabel moneySentLabel;
	JLabel moneyReceivedLabel;
	IntContainer moneySent;
	IntContainer moneyReceived;
	JButton pChange;
	JButton oChange;
	JPanel oProp, pProp;//cur player's or opponent's props tradable
	private int returnValue;
	private AskDialog ad;//ask dialog
	
	ArrayList<String> reqestedProps,offeredProps;
	ArrayList<BooleanContainer> isCTradedDarkPurple, isCTradedLightBlue, isCTradedLightPurple,
	 isCTradedOrange,isCTradedRed,isCTradedYellow,isCTradedGreen,
	 isCTradedDarkBlue,isCTradedUtility, isCTradedRailroad,
	 isOTradedDarkPurple, isOTradedLightBlue, isOTradedLightPurple,
	 isOTradedOrange,isOTradedRed,isOTradedYellow,isOTradedGreen,
	 isOTradedDarkBlue,isOTradedUtility, isOTradedRailroad;
	
	TradeDialog(Player curPlayer, Player opponent){
		System.out.println(curPlayer.getPlayerName() + " " + opponent.getPlayerName());
		//check if the opponent has at least one property
		if(opponent.getDarkPurpleSize() == 0 &&
				opponent.getLightBlueSize() == 0 &&
				opponent.getLightPurpleSize() == 0 &&
				opponent.getOrangeSize() == 0 &&
				opponent.getRedSize() == 0 &&
				opponent.getYellowSize() == 0 &&
				opponent.getGreenSize() == 0 &&
				opponent.getDarkBlueSize() == 0 &&
				opponent.getUtilitySize() == 0 &&
				opponent.getRailroadSize() == 0){
			JLabel warning = new JLabel(
					opponent.getPlayerName() + " does not have any property at all. You cannot trade.");
			JOptionPane.showMessageDialog(this, warning);
			returnValue = 0;
			this.setVisible(false);
			
		}
		else{
		
		returnValue = -1;
		
		isCTradedDarkPurple = new ArrayList(2);
		isCTradedLightBlue = new ArrayList(3);
		isCTradedLightPurple = new ArrayList(3);
		isCTradedOrange = new ArrayList(3);
		isCTradedRed = new ArrayList(3);
		isCTradedYellow = new ArrayList(3);
		isCTradedGreen = new ArrayList(3);
		isCTradedDarkBlue = new ArrayList(2);
		isCTradedRailroad = new ArrayList(4);
		isCTradedUtility = new ArrayList(2);
		
		isOTradedDarkPurple = new ArrayList(2);
		isOTradedLightBlue = new ArrayList(3);
		isOTradedLightPurple = new ArrayList(3);
		isOTradedOrange = new ArrayList(3);
		isOTradedRed = new ArrayList(3);
		isOTradedYellow = new ArrayList(3);
		isOTradedGreen = new ArrayList(3);
		isOTradedDarkBlue = new ArrayList(2);
		isOTradedRailroad = new ArrayList(4);
		isOTradedUtility = new ArrayList(2);
		
		moneySentLabel = new JLabel("cash: $" + 0);
		moneyReceivedLabel = new JLabel("cash: $" + 0);
		pChange = new JButton("change");
		oChange = new JButton("change");
		pChange.addActionListener(this);
		oChange.addActionListener(this);
		JPanel pCashPanel = new JPanel(new FlowLayout());
		JPanel oCashPanel = new JPanel(new FlowLayout());
		pCashPanel.setBackground(Color.white);
		oCashPanel.setBackground(Color.white);
		pCashPanel.add(pChange);
		pCashPanel.add(moneySentLabel);
		oCashPanel.add(oChange);
		oCashPanel.add(moneyReceivedLabel);
		
		cp = curPlayer;
		op = opponent;
		
		
		playerTotalGain = 0;
		opTotalGain = 0;
		
		moneySent = new IntContainer(0);
		moneyReceived = new IntContainer(0);
		
		request = new JButton("Request");
		cancel = new JButton("Cancel");
		request.addActionListener(this);
		cancel.addActionListener(this);
		
		//panelOnTradeDialog -- JPanel that has 
		/*1. splitPane(JSplitPane) on center -- it has two scroll panel on which the tradable props displayed and selected.
		 *2. buttonPanel that has two panels on south
		 *3. moneyPanel showing player name and cash on east.
		 */
		panelOnTradeDialog = new JPanel(new BorderLayout());
		panelOnTradeDialog.setBackground(Color.white);
		/*
		pProp = new JPanel(new GridLayout(0,1));
		oProp = new JPanel(new GridLayout(0,1));
		*/
		pProp = new JPanel();
		oProp = new JPanel();
		BoxLayout boxl = new BoxLayout(pProp, BoxLayout.Y_AXIS);
		BoxLayout boxl2 = new BoxLayout(oProp, BoxLayout.Y_AXIS);
		pProp.setLayout(boxl);
		oProp.setLayout(boxl2);
		
		pProp.add(pCashPanel);
		oProp.add(oCashPanel);
		/*makePropList(cp, pProp, moneySentLabel, pChange);
		makePropList(op, oProp, moneyReceivedLabel, oChange);*/
		
		JScrollPane tPane = new JScrollPane(pProp, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//top pane for the active player
		JScrollPane bPane = new JScrollPane(oProp, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//bottom pane for the opponent
		
		//add available properties.
		makeItemList(cp, pProp);
		makeItemList(op, oProp);
		//pProp.setAlignmentX(LEFT_ALIGNMENT);
		//oProp.setAlignmentX(LEFT_ALIGNMENT);
		
		propPanel = new JPanel();
		BoxLayout bLayout = new BoxLayout(propPanel, BoxLayout.Y_AXIS);
		propPanel.setLayout(bLayout);
		propPanel.add(new JLabel("You want to give away:"));
		propPanel.add(tPane);
		propPanel.add(new JLabel("You would like to get:"));
		propPanel.add(bPane);
		propPanel.setVisible(true);
		
		//buttomPanel
		JPanel buttomPanel = new JPanel(new FlowLayout());
		buttomPanel.add(request);
		buttomPanel.add(cancel);
		
		//moneyPanel
		JPanel moneyPanel = new JPanel();
		BoxLayout bl = new BoxLayout(moneyPanel, BoxLayout.Y_AXIS);
		moneyPanel.setLayout(bl);
		
		moneyPanel.add(Box.createRigidArea(new Dimension(20,30)));
		moneyPanel.add(new JLabel(cp.getPlayerName()));
		moneyPanel.add(Box.createRigidArea(new Dimension(20,10)));
		moneyPanel.add(new JLabel("starting cash: " + cp.getCash()));
		moneyPanel.add(Box.createRigidArea(new Dimension(20,10)));
		moneyPanel.add(new JLabel(op.getPlayerName()));
		moneyPanel.add(Box.createRigidArea(new Dimension(20,10)));
		moneyPanel.add(new JLabel("starting cash: " + op.getCash()));
		
		
		//add components and other operation to display
		this.add(panelOnTradeDialog);
		panelOnTradeDialog.add(propPanel, BorderLayout.CENTER);
		panelOnTradeDialog.add(buttomPanel, BorderLayout.SOUTH);
		panelOnTradeDialog.add(moneyPanel, BorderLayout.EAST);
		setSize(400, 400);
		setModal(true);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		}
	}
	public void actionPerformed(ActionEvent e){
		System.out.println("buttonpressed");
		this.reqestedProps = new ArrayList();
		this.offeredProps = new ArrayList();
        if(e.getSource() == request){
        	//at first, need to check if at least one of opponent's props is checked.
        	//
        	for(int i = 0; i < this.isOTradedDarkPurple.size(); i++)
        	{
        		if(this.isOTradedDarkPurple.get(i).boolValue() == true)
        		{
        			this.reqestedProps.add(this.op.getDarkPurpleNameAt(i));
        		}
        	}
        	for(int i = 0; i < this.isOTradedLightBlue.size(); i++)
        	{
        		if(this.isOTradedLightBlue.get(i).boolValue() == true)
        		{
        			this.reqestedProps.add(this.op.getLightBlueNameAt(i));
        		}
        	}
        	for(int i = 0; i < this.isOTradedLightPurple.size(); i++)
        	{
        		if(this.isOTradedLightPurple.get(i).boolValue() == true)
        		{
        			this.reqestedProps.add(this.op.getLightPurpleNameAt(i));
        		}
        	}
        	for(int i = 0; i < this.isOTradedOrange.size(); i++)
        	{
        		if(this.isOTradedOrange.get(i).boolValue() == true)
        		{
        			this.reqestedProps.add(this.op.getDarkPurpleNameAt(i));
        		}
        	}
        	for(int i = 0; i < this.isOTradedRed.size(); i++)
        	{
        		if(this.isOTradedRed.get(i).boolValue() == true)
        		{
        			this.reqestedProps.add(this.op.getRedNameAt(i));
        		}
        	}
        	for(int i = 0; i < this.isOTradedYellow.size(); i++)
        	{
        		if(this.isOTradedYellow.get(i).boolValue() == true)
        		{
        			this.reqestedProps.add(this.op.getYellowNameAt(i));
        		}
        	}
        	for(int i = 0; i < this.isOTradedGreen.size(); i++)
        	{
        		if(this.isOTradedGreen.get(i).boolValue() == true)
        		{
        			this.reqestedProps.add(this.op.getGreenNameAt(i));
        		}
        	}
        	for(int i = 0; i < this.isOTradedDarkBlue.size(); i++)
        	{
        		if(this.isOTradedDarkBlue.get(i).boolValue() == true)
        		{
        			this.reqestedProps.add(this.op.getDarkBlueNameAt(i));
        		}
        	}
        	for(int i = 0; i < this.isOTradedRailroad.size(); i++)
        	{
        		if(this.isOTradedRailroad.get(i).boolValue() == true)
        		{
        			this.reqestedProps.add(this.op.getRailroadNameAt(i));
        		}
        	}
        	for(int i = 0; i < this.isOTradedUtility.size(); i++)
        	{
        		if(this.isOTradedUtility.get(i).boolValue() == true)
        		{
        			this.reqestedProps.add(this.op.getUtilityNameAt(i));
        		}
        	}
        	if(reqestedProps.size() != 0){
        		for(int i = 0; i < this.isCTradedDarkPurple.size(); i++)
            		if(this.isCTradedDarkPurple.get(i).boolValue() == true)
            		{
            			this.offeredProps.add(this.cp.getDarkPurpleNameAt(i));
            		}
            	}
            	for(int i = 0; i < this.isCTradedLightBlue.size(); i++)
            	{
            		if(this.isCTradedLightBlue.get(i).boolValue() == true)
            		{
            			this.offeredProps.add(this.cp.getLightBlueNameAt(i));
            		}
            	}
            	for(int i = 0; i < this.isCTradedLightPurple.size(); i++)
            	{
            		if(this.isCTradedLightPurple.get(i).boolValue() == true)
            		{
            			this.offeredProps.add(this.cp.getLightPurpleNameAt(i));
            		}
            	}
            	for(int i = 0; i < this.isCTradedOrange.size(); i++)
            	{
            		if(this.isCTradedOrange.get(i).boolValue() == true)
            		{
            			this.offeredProps.add(this.cp.getDarkPurpleNameAt(i));
            		}
            	}
            	for(int i = 0; i < this.isCTradedRed.size(); i++)
            	{
            		if(this.isCTradedRed.get(i).boolValue() == true)
            		{
            			this.offeredProps.add(this.cp.getRedNameAt(i));
            		}
            	}
            	for(int i = 0; i < this.isCTradedYellow.size(); i++)
            	{
            		if(this.isCTradedYellow.get(i).boolValue() == true)
            		{
            			this.offeredProps.add(this.cp.getYellowNameAt(i));
            		}
            	}
            	for(int i = 0; i < this.isCTradedGreen.size(); i++)
            	{
            		if(this.isCTradedGreen.get(i).boolValue() == true)
            		{
            			this.offeredProps.add(this.cp.getGreenNameAt(i));
            		}
            	}
            	for(int i = 0; i < this.isCTradedDarkBlue.size(); i++)
            	{
            		if(this.isCTradedDarkBlue.get(i).boolValue() == true)
            		{
            			this.offeredProps.add(this.cp.getDarkBlueNameAt(i));
            			System.out.println("Added");
            		}
            	}
            	for(int i = 0; i < this.isCTradedRailroad.size(); i++)
            	{
            		if(this.isCTradedRailroad.get(i).boolValue() == true)
            		{
            			this.offeredProps.add(this.cp.getRailroadNameAt(i));
            		}
            	}
            	for(int i = 0; i < this.isCTradedUtility.size(); i++)
            	{
            		if(this.isCTradedUtility.get(i).boolValue() == true)
            		{
            			this.offeredProps.add(this.cp.getUtilityNameAt(i));
            		}
            	}
            	
            	if(offeredProps.size() != 0 || moneySent.intValue() != 0){
            		new AskDialog(cp, op, this.reqestedProps, offeredProps, moneySent, moneyReceived, this);
            	}
        	}
        	//if requestedProps == 0, nothing happens.
	
        else if(e.getSource() == cancel){
        	setVisible(false);
        }
        else if(e.getSource() == pChange){
        	new EnterAmount(moneySent, moneySentLabel, true, cp.getCash());
        	
        }
        else if(e.getSource() == oChange){
        	new EnterAmount(moneyReceived, moneyReceivedLabel, false, op.getCash());
        	
        }
    }
	public void makePropList(Player p, JPanel pList, JLabel l, JButton b){
		l = new JLabel("cash: $" + 0);
		b = new JButton("Change");
		
		
		JPanel cashPanel = new JPanel(new FlowLayout());
		cashPanel.add(l);
		cashPanel.add(b);
		
		pList.add(cashPanel);
	}
	/**********makeItemList*****************************
	 * finds what items can be traded by looking at player's props
	 *  then adds them to pnl
	 *  Plus, at the same time, check if the opponent has some prop group that has no building.
	 */
	public void makeItemList(Player p, JPanel pnl){
		/*Process: check if there is a houses of a color
		if so skip adding them to the panel(represents list)
		if not, add all the properties to the panel.*/
		
		
		Boolean canTrade = false;//if it is still false finally, the player every prop group has houses(s).if so, terminate this dialog
		BooleanContainer hasHouse = new BooleanContainer(false);//If there is NO house/hotel on the property group, can be traded.
		for(int i = 0; i<p.getDarkPurpleSize() && hasHouse.boolValue() == false ;i++){
			if(p.getNumberOfHouses(p.getDarkPurpleNameAt(i)) != 0){
				hasHouse.setBoolValue(true);
			}
			else{
				if(p == cp)
					this.isCTradedDarkPurple.add(new BooleanContainer(false));
				else
					this.isOTradedDarkPurple.add(new BooleanContainer(false));
				System.out.println("added.");
			}
		}
		if(hasHouse.boolValue() == false){//if there is no house on the prop group.
			canTrade = true;
			for(int i = 0; i<p.getDarkPurpleSize();i++){
				String n = p.getDarkPurpleNameAt(i);
				if(p == cp)
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isCTradedDarkPurple.get(i)));
				else
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isOTradedDarkPurple.get(i)));
			}
		}
		hasHouse.setBoolValue(false);//reset
		for(int i = 0; i<p.getLightBlueSize() && hasHouse.boolValue() == false ;i++){
			if(p.getNumberOfHouses(p.getLightBlueNameAt(i)) != 0){
				hasHouse.setBoolValue(true);
			}
			else{
				if(p == cp)
					this.isCTradedLightBlue.add(new BooleanContainer(false));
				else
					this.isOTradedLightBlue.add(new BooleanContainer(false));
			}
		}
		if(hasHouse.boolValue() == false){//if there is no house on the prop group.
			canTrade = true;
			for(int i = 0; i<p.getLightBlueSize();i++){
				String n = p.getLightBlueNameAt(i);
				
				
				if(p == cp)
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isCTradedLightBlue.get(i)));
				else
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isOTradedLightBlue.get(i)));
			}
		}
		hasHouse.setBoolValue(false);//reset
		for(int i = 0; i<p.getLightPurpleSize() && hasHouse.boolValue() == false ;i++){
			if(p.getNumberOfHouses(p.getLightPurpleNameAt(i)) != 0){
				hasHouse.setBoolValue(true);
			}
			else{
				if(p == cp)
					this.isCTradedLightPurple.add(new BooleanContainer(false));
				else
					this.isOTradedLightPurple.add(new BooleanContainer(false));
			}
		}
		if(hasHouse.boolValue() == false){//if there is no house on the prop group.
			canTrade = true;
			for(int i = 0; i<p.getLightPurpleSize();i++){
				String n = p.getLightPurpleNameAt(i);
				
				if(p == cp)
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isCTradedLightPurple.get(i)));
				else
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isOTradedLightPurple.get(i)));
			}
		}
		hasHouse.setBoolValue(false);//reset
		for(int i = 0; i<p.getOrangeSize() && hasHouse.boolValue() == false ;i++){
			if(p.getNumberOfHouses(p.getOrangeNameAt(i)) != 0){
				hasHouse.setBoolValue(true);
			}
			else{
				if(p == cp)
					this.isCTradedOrange.add(new BooleanContainer(false));
				else
					this.isOTradedOrange.add(new BooleanContainer(false));
				
			}
		}
		if(hasHouse.boolValue() == false){//if there is no house on the prop group.
			canTrade = true;
			for(int i = 0; i<p.getOrangeSize();i++){
				String n = p.getOrangeNameAt(i);
				
				if(p == cp)
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isCTradedOrange.get(i)));
				else
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isOTradedOrange.get(i)));
			}
		}
		hasHouse.setBoolValue(false);//reset
		for(int i = 0; i<p.getRedSize() && hasHouse.boolValue() == false ;i++){
			if(p.getNumberOfHouses(p.getRedNameAt(i)) != 0){
				hasHouse.setBoolValue(true);
			}
			else{
				if(p == cp)
					this.isCTradedRed.add(new BooleanContainer(false));
				else
					this.isOTradedRed.add(new BooleanContainer(false));
			}
		}
		if(hasHouse.boolValue() == false){//if there is no house on the prop group.
			canTrade = true;
			for(int i = 0; i<p.getRedSize();i++){
				String n = p.getRedNameAt(i);
				
				if(p == cp)
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isCTradedRed.get(i)));
				else
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isOTradedRed.get(i)));
			}
		}
		hasHouse.setBoolValue(false);//reset
		for(int i = 0; i<p.getYellowSize() && hasHouse.boolValue() == false ;i++){
			if(p.getNumberOfHouses(p.getYellowNameAt(i)) != 0){
				hasHouse.setBoolValue(true);
			}
			else{
				if(p == cp)
					this.isCTradedYellow.add(new BooleanContainer(false));
				else
					this.isOTradedYellow.add(new BooleanContainer(false));
				
			}
		}
		if(hasHouse.boolValue() == false){//if there is no house on the prop group.
			canTrade = true;
			for(int i = 0; i<p.getYellowSize();i++){
				String n = p.getYellowNameAt(i);
				
				if(p == cp)
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isCTradedYellow.get(i)));
				else
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isOTradedYellow.get(i)));
			}
		}
		hasHouse.setBoolValue(false);//reset
		for(int i = 0; i<p.getGreenSize() && hasHouse.boolValue() == false ;i++){
			if(p.getNumberOfHouses(p.getGreenNameAt(i)) != 0){
				hasHouse.setBoolValue(true);
			}
			else{
				if(p == cp)
					this.isCTradedGreen.add(new BooleanContainer(false));
				else
					this.isOTradedGreen.add(new BooleanContainer(false));
				System.out.println("added.");
			}
		}
		if(hasHouse.boolValue() == false){//if there is no house on the prop group.
			canTrade = true;
			for(int i = 0; i<p.getGreenSize();i++){
				String n = p.getLightBlueNameAt(i);
				
				if(p == cp)
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isCTradedGreen.get(i)));
				else
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isOTradedGreen.get(i)));
			}
		}
		hasHouse.setBoolValue(false);//reset
		for(int i = 0; i<p.getDarkBlueSize() && hasHouse.boolValue() == false ;i++){
			if(p.getNumberOfHouses(p.getDarkBlueNameAt(i)) != 0){
				hasHouse.setBoolValue(true);
			}
			else{
				if(p == cp)
					this.isCTradedDarkBlue.add(new BooleanContainer(false));
				else
					this.isOTradedDarkBlue.add(new BooleanContainer(false));
				
			}
		}
		if(hasHouse.boolValue() == false){//if there is no house on the prop group.
			canTrade = true;
			for(int i = 0; i<p.getDarkBlueSize();i++){
				String n = p.getDarkBlueNameAt(i);
				
				if(p == cp)
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isCTradedDarkBlue.get(i)));
				else
					pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isOTradedDarkBlue.get(i)));
			}
		}
		hasHouse.setBoolValue(false);//reset
		for(int i = 0; i<p.getUtilitySize();i++){
			if(p == cp)
				this.isCTradedUtility.add(new BooleanContainer(false));
			else
				this.isOTradedUtility.add(new BooleanContainer(false));
		}
		for(int i = 0; i<p.getUtilitySize();i++){
			canTrade = true;
			String n = p.getUtilityNameAt(i);
			if(p == cp)
				pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isCTradedUtility.get(i)));
			else
				pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isOTradedUtility.get(i)));
		}
		
		hasHouse.setBoolValue(false);//reset
		for(int i = 0; i<p.getRailroadSize() ;i++){
			if(p == cp)
				this.isCTradedRailroad.add(new BooleanContainer(false));
			else
				this.isOTradedRailroad.add(new BooleanContainer(false));
		}
		
		for(int i = 0; i<p.getRailroadSize();i++){
			canTrade = true;
			String n = p.getRailroadNameAt(i);	
			if(p == cp)
				pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isCTradedRailroad.get(i)));
			else
				pnl.add(new TradeItem(p, n, p.isMortgaged(n), this.isOTradedRailroad.get(i)));
		}
		pnl.add(Box.createRigidArea(new Dimension(10, 10)));
		pnl.add(Box.createRigidArea(new Dimension(10, 10)));
		pnl.add(Box.createRigidArea(new Dimension(10, 10)));
		pnl.add(Box.createRigidArea(new Dimension(10, 10)));
		pnl.add(Box.createRigidArea(new Dimension(10, 10)));
		//If the opponent has no prop to be traded.
		if(canTrade == false && p == op){
			JLabel warning = new JLabel(
					"Every " + op.getPlayerName() + "'s property group has building. You cannot trade with the player.");
			JOptionPane.showMessageDialog(this, warning);
			returnValue = 0;
			this.setVisible(false);
		}
	}

	
	
}

/*EnterAmount - JDialog obj on which there is mainPanel(JPanel) that has
 *  (JLabel)tField -- displays the amount of cash to be traded.
 *  (JPanel)okCancelPanel -- has two buttons ok and cancel
 *  (JPanel)numPad -- has buttons to input the number
 */
class EnterAmount extends JDialog implements ActionListener{
		private JButton ok, cancel, b1, b2, b3, b4, b5, b6, b7,
		b8, b9, b0, bCl;
		private String stringNum;
		JLabel tField;
		IntContainer cash;//changeable by input
		JLabel cashLabel;
		int curCash;//amount of cash the player has.
		JLabel warning, warning2;
		Boolean isCurrentPlayer;
	EnterAmount(IntContainer num, JLabel l, boolean isCurPlayer, int originalCash){
		
		cash = num;
		curCash = originalCash;
		cashLabel = l;
		warning = new JLabel("       ");
		warning2 = new JLabel("       ");
		warning.setAlignmentX(CENTER_ALIGNMENT);
		warning2.setAlignmentX(CENTER_ALIGNMENT);
		isCurrentPlayer = isCurPlayer;
		JPanel mainPanel = new JPanel();
		BoxLayout bl = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
		mainPanel.setLayout(bl);
		
		
		//tField(JLabel) -- displays the amount of cash to be traded.
		stringNum = "" + num.intValue();
		tField = new JLabel("Amount: $" + stringNum);
		tField.setAlignmentX(CENTER_ALIGNMENT);
		
		//okCancelPanel -- has two buttons ok and cancel
		JPanel okCancelPanel = new JPanel(new FlowLayout());
		ok = new JButton("ok");
		cancel = new JButton("cancel");
		ok.addActionListener(this);
		cancel.addActionListener(this);
		okCancelPanel.add(ok);
		okCancelPanel.add(cancel);
		
		//CENTER: numPad -- has buttons to input the number
		JPanel numPad = new JPanel(new GridLayout(4,3));
		b1 = new JButton("1");
		b2 = new JButton("2");
		b3 = new JButton("3");
		b4 = new JButton("4");
		b5 = new JButton("5");
		b6 = new JButton("6");
		b7 = new JButton("7");
		b8 = new JButton("8");
		b9 = new JButton("9");
		b0 = new JButton("0");
		bCl = new JButton("clear");
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		b5.addActionListener(this);
		b6.addActionListener(this);
		b7.addActionListener(this);
		b8.addActionListener(this);
		b9.addActionListener(this);
		b0.addActionListener(this);
		bCl.addActionListener(this);
		numPad.add(b1);
		numPad.add(b2);
		numPad.add(b3);
		numPad.add(b4);
		numPad.add(b5);
		numPad.add(b6);
		numPad.add(b7);
		numPad.add(b8);
		numPad.add(b9);
		numPad.add(b0);
		numPad.add(bCl);
		
		JLabel inst1, inst2;
		if(isCurPlayer == true){
			inst1 = new JLabel("How much cash do you");
			inst2 = new JLabel("want to offer?");
			inst1.setAlignmentX(CENTER_ALIGNMENT);
			inst2.setAlignmentX(CENTER_ALIGNMENT);
			mainPanel.add(inst1);
			mainPanel.add(inst2);
		}
		else{
			inst1 = new JLabel("How much cash");
			inst2 = new JLabel("do you want?");
			inst1.setAlignmentX(CENTER_ALIGNMENT);
			inst2.setAlignmentX(CENTER_ALIGNMENT);
			mainPanel.add(inst1);
			mainPanel.add(inst2);
		}
		mainPanel.add(Box.createRigidArea(new Dimension(10, 10)));
		mainPanel.add(tField);
		mainPanel.add(Box.createRigidArea(new Dimension(10, 10)));
		mainPanel.add(numPad);
		mainPanel.add(okCancelPanel);
		mainPanel.add(warning);
		mainPanel.add(warning2);
		
		this.add(mainPanel);
		this.setSize(200, 300);
		this.setLocationRelativeTo(null);
		setModal(true);
		this.setVisible(true);
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == ok){
			if(Integer.parseInt(stringNum) <= curCash){
				this.cash.setIntValue(Integer.parseInt(stringNum));
				cashLabel.setText("cash: $" + cash.intValue());
				this.setVisible(false);
			}
			else{
				if(isCurrentPlayer == true){
					warning.setText("Offered cash exceeds");
					warning2.setText("Your capacity.");
				}
				else{
					warning.setText("The requested cash exceeds");
					warning2.setText("his/her capacity.");
				}
			}
			
		
		}
		else if(e.getSource() == cancel){
			this.setVisible(false);
		}
		else if(e.getSource() == b1){
			if(stringNum.equals("0"))
				stringNum = "";
			stringNum += "1";
			tField.setText("Amount: $" + stringNum);
		}
		else if(e.getSource() == b2){
			if(stringNum.equals("0"))
				stringNum = "";
			stringNum += "2";
			tField.setText("Amount: $" + stringNum);
		}
		else if(e.getSource() == b3){
			if(stringNum.equals("0"))
				stringNum = "";
			stringNum += "3";
			tField.setText("Amount: $" + stringNum);
		}
		else if(e.getSource() == b4){
			if(stringNum.equals("0"))
				stringNum = "";
			stringNum += "4";
			tField.setText("Amount: $" + stringNum);
		}
		else if(e.getSource() == b5){
			if(stringNum.equals("0"))
				stringNum = "";
			stringNum += "5";
			tField.setText("Amount: $" + stringNum);
		}
		else if(e.getSource() == b6){
			if(stringNum.equals("0"))
				stringNum = "";
			stringNum += "6";
			tField.setText("Amount: $" + stringNum);
		}
		else if(e.getSource() == b7){
			if(stringNum.equals("0"))
				stringNum = "";
			stringNum += "7";
			tField.setText("Amount: $" + stringNum);
		}
		else if(e.getSource() == b8){
			if(stringNum.equals("0"))
				stringNum = "";
			stringNum += "8";
			tField.setText("Amount: $" + stringNum);
		}
		else if(e.getSource() == b9){
			if(stringNum.equals("0"))
				stringNum = "";
			stringNum += "9";
			tField.setText("Amount: $" + stringNum);
		}	
		else if(e.getSource() == b0){
			if(stringNum.equals("0") == false){
				stringNum += "0";
				tField.setText("Amount: $" + stringNum);
			}
		}
		else if(e.getSource() == bCl){
			stringNum = "0";
			tField.setText("Amount: $" + stringNum);
		}
		
	}
}

class TradeItem extends JPanel implements ActionListener{
	private JLabel nameLabel;
	private JLabel MortgagedLabel;//says mortgaged or not
	private JCheckBox checkBox;//if want to trade, it is checked.
	private Player player;
	private String nameOfProperty;
	private BooleanContainer traded;
	
	TradeItem(Player p, String n, Boolean mortgaged, BooleanContainer isTraded){
		
		player = p;
		nameOfProperty = n;
		checkBox = new JCheckBox();
		checkBox.addActionListener(this);
		traded = isTraded;
		
		if(mortgaged == true){
			MortgagedLabel = new JLabel("Mortgaged");
		}
		else
			MortgagedLabel = new JLabel("");
		nameLabel = this.adjustName(nameOfProperty);
		
		checkBox.setAlignmentX(LEFT_ALIGNMENT);
		nameLabel.setAlignmentX(LEFT_ALIGNMENT);
		MortgagedLabel.setAlignmentX(LEFT_ALIGNMENT);
		add(checkBox);
		add(nameLabel);
		add(MortgagedLabel);
		//this.setBackground(Color.black);
		//this.setAlignmentX(LEFT_ALIGNMENT);
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == checkBox){
			if(checkBox.isSelected() == true){
				System.out.println("will be traded");
				traded.setBoolValue(true);
			}
			else
				traded.setBoolValue(false);
		}
	}
	public JLabel adjustName(String str){
		JLabel adjustedName = new JLabel();
		if(str == "MediterraneanAve"){
			adjustedName = new JLabel("Mediterranean Avenue");
			adjustedName.setForeground(Color.getHSBColor((float)0.9,(float)0.9,(float)0.7));
		}
		else if (str == "BalticAve"){
			adjustedName = new JLabel("       Baltic Avenue");
			adjustedName.setForeground(Color.getHSBColor((float)0.9,(float)0.9,(float)0.7));
		}
		else if (str == "OrientalAve"){
			adjustedName = new JLabel("     Oriental Avenue");
			adjustedName.setForeground(Color.getHSBColor((float)0.6,(float)1,(float)1));
		}
		else if (str == "VermontAve"){
			adjustedName = new JLabel("      Vermont Avenue");
			adjustedName.setForeground(Color.getHSBColor((float)0.6,(float)1,(float)1));
		}
		else if (str == "ConnecticutAve"){
			adjustedName = new JLabel("  Connecticut Avenue");
			adjustedName.setForeground(Color.getHSBColor((float)0.6,(float)1,(float)1));
		}
		else if (str == "StCharlesPlace"){
			adjustedName = new JLabel("    St.Charles Place");
			adjustedName.setForeground(Color.magenta);
		}
		else if (str == "StatesAve"){
			adjustedName = new JLabel("        States Avenue");
			adjustedName.setForeground(Color.magenta);
		}
		else if (str == "VirginiaAve"){
			adjustedName = new JLabel("     Virginia Avenue");
			adjustedName.setForeground(Color.magenta);
		}
		else if (str == "StJamesPlace"){
			adjustedName = new JLabel("      St.James Place");
			adjustedName.setForeground(Color.orange);
		}
		else if (str == "TennesseeAve"){
			adjustedName = new JLabel("    Tennessee Avenue");
			adjustedName.setForeground(Color.orange);
		}
		else if (str == "NewYorkAve"){
			adjustedName = new JLabel("      NewYork Avenue");
			adjustedName.setForeground(Color.orange);
		}
		else if(str == "KentuckyAve"){
			adjustedName = new JLabel("     Kentucky Avenue");
			adjustedName.setForeground(Color.red);
		}
		else if(str == "IndianaAve"){
			adjustedName = new JLabel("      Indiana Avenue");
			adjustedName.setForeground(Color.red);
		}
		else if(str == "IllinoisAve"){
			adjustedName = new JLabel("     Illinois Avenue");
			adjustedName.setForeground(Color.red);
		}
		else if(str == "AtlanticAve"){
			adjustedName = new JLabel("     Atlantic Avenue");
			adjustedName.setForeground(Color.yellow);
		}
		else if(str == "VentnorAve"){
			adjustedName = new JLabel("      Ventnor Avenue");
			adjustedName.setForeground(Color.yellow);
		}
		else if(str == "MarvinGardens"){
			adjustedName = new JLabel("      Marvin Gardens");
			adjustedName.setForeground(Color.yellow);
		}
		else if(str == "PacificAve"){
			adjustedName = new JLabel("      Pacific Avenue");
			adjustedName.setForeground(Color.green);
		}
		else if(str == "NorthCarolinaAve"){
			adjustedName = new JLabel("NorthCarolina Avenue");
			adjustedName.setForeground(Color.green);
		}
		else if(str == "PennsylvaniaAve"){
			adjustedName = new JLabel(" Pennsylvania Avenue");
			adjustedName.setForeground(Color.green);
		}
		else if(str == "ParkPlace"){
			adjustedName = new JLabel("          Park Place");
			adjustedName.setForeground(Color.blue);
		}
		else if(str == "Boardwalk"){
			adjustedName = new JLabel("           Boardwalk");
			adjustedName.setForeground(Color.blue);
		}
		else if(str == "WaterWorks"){
			adjustedName = new JLabel("Water Works");
			adjustedName.setForeground(Color.darkGray);
		}
		else if(str == "ElectricCompany"){
			adjustedName = new JLabel("Electric Company");
			adjustedName.setForeground(Color.darkGray);
		}
		else if(str == "ReadingRailroad"){
			adjustedName = new JLabel("Reading Railroad");
			adjustedName.setForeground(Color.black);
		}
		else if(str == "B&ORailroad"){
			adjustedName = new JLabel("B&O Railroad");
			adjustedName.setForeground(Color.black);
		}
		else if(str == "ShortLineRailroad"){
			adjustedName = new JLabel("ShortLine Railroad");
			adjustedName.setForeground(Color.black);
		}
		else if(str == "PennsylvaniaRailroad"){
			adjustedName = new JLabel("Pennsylvania Railroad");
			adjustedName.setForeground(Color.black);
		}
		
		return adjustedName;
	}
}
class AskDialog extends JDialog implements ActionListener{
	Player cp, op;
	ArrayList<String> rl, ol;//request list and offered list
	JPanel panelWithButtons;
	JButton accept, decline;
	IntContainer moneySent, moneyReceived;
	TradeDialog td;
	AskDialog(Player curPlayer, Player opponent, ArrayList<String> requestList, ArrayList<String> offerList,
			IntContainer sentMoney, IntContainer receivedMoney, TradeDialog tradeDialog){
		cp = curPlayer;
		op = opponent;
		rl = requestList;
		ol = offerList;
		
		td = tradeDialog;
		
		moneySent = sentMoney;
		moneyReceived = receivedMoney;
		
		JPanel northPanel = new JPanel(new GridLayout(1, 3));
		JPanel mainPanel = new JPanel(new BorderLayout());//has listPanel and panelWithButtons
		JPanel listPanel = new JPanel(new GridLayout(0,2));//goes to center
		panelWithButtons = new JPanel();//goes to east
		
		//make northPanel 
		JLabel label = new JLabel("You will give away:");
		//label.setAlignmentX(LEFT_ALIGNMENT);
		northPanel.add(label);
		northPanel.add(new JLabel("   " + cp.getPlayerName() + " offers:"));
		northPanel.add(new JLabel());
		
		//make listPanel that has two list, props offered and requested.
		JPanel pProp = new JPanel();
		JPanel oProp = new JPanel();
		BoxLayout bl = new BoxLayout(pProp, BoxLayout.Y_AXIS);
		BoxLayout bl2 = new BoxLayout(oProp, BoxLayout.Y_AXIS);
		pProp.setLayout(bl);
		oProp.setLayout(bl2);
		pProp.add(new JLabel("Cash: " + moneySent.intValue() ));
		oProp.add(new JLabel("Cash: " + moneyReceived.intValue() ));
		makeList(pProp, rl);
		makeList(oProp, ol);
		JScrollPane cPane = new JScrollPane(pProp, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		JScrollPane oPane = new JScrollPane(oProp, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		listPanel.add(oPane);
		listPanel.add(cPane);
		listPanel.setVisible(true);
		
		//make panelWithButtons
		BoxLayout bl3 = new BoxLayout(panelWithButtons, BoxLayout.Y_AXIS);
		panelWithButtons.setLayout(bl3);
		panelWithButtons.add(new JLabel("Do you accept  "));
		panelWithButtons.add(new JLabel("this offer?  "));
		panelWithButtons.add(Box.createRigidArea(new Dimension(20,10)));
		accept = new JButton("Accept");
		decline = new JButton("Decline");
		accept.addActionListener(this);
		decline.addActionListener(this);
		panelWithButtons.add(accept);
		panelWithButtons.add(Box.createRigidArea(new Dimension(20,10)));
		panelWithButtons.add(decline);

		this.add(northPanel, BorderLayout.NORTH);
		this.add(listPanel, BorderLayout.CENTER);
		this.add(panelWithButtons, BorderLayout.EAST);

		setSize(400, 200);
		setModal(true);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == accept){
			for(int i = 0; i< rl.size(); i++){
				this.cp.addNewProperty(rl.get(i));
				this.op.removeProperty(rl.get(i));
			}
			for(int i = 0; i<ol.size(); i++){
				this.cp.removeProperty(ol.get(i));
				this.op.removeProperty(ol.get(i));
			}
			cp.subtractCash(moneySent.intValue());
			cp.addCash(moneyReceived.intValue());
			op.subtractCash(moneyReceived.intValue());
			op.addCash(moneySent.intValue());
			
			td.setVisible(false);
			this.setVisible(false);
		}
		else if(e.getSource() == decline){
			
			this.setVisible(false);
		}
		
	}
	public void makeList(JPanel p, ArrayList<String> l){
		for(int i = 0; i<l.size();i++){
			System.out.println(l.get(i));
			JLabel label = adjustName(l.get(i));
			//JPanel panel = new JPanel();
			label.setAlignmentX(LEFT_ALIGNMENT);
			//panel.add(label);*/
			p.add(label);
			//panel.setBackground(Color.black);
		}
		
	}
	public JLabel adjustName(String str){
		JLabel adjustedName = new JLabel();
		if(str == "MediterraneanAve"){
			adjustedName = new JLabel("Mediterranean Avenue");
			adjustedName.setForeground(Color.getHSBColor((float)0.9,(float)0.9,(float)0.7));
		}
		else if (str == "BalticAve"){
			adjustedName = new JLabel("Baltic Avenue");
			adjustedName.setForeground(Color.getHSBColor((float)0.9,(float)0.9,(float)0.7));
		}
		else if (str == "OrientalAve"){
			adjustedName = new JLabel("Oriental Avenue");
			adjustedName.setForeground(Color.getHSBColor((float)0.6,(float)1,(float)1));
		}
		else if (str == "VermontAve"){
			adjustedName = new JLabel("Vermont Avenue");
			adjustedName.setForeground(Color.getHSBColor((float)0.6,(float)1,(float)1));
		}
		else if (str == "ConnecticutAve"){
			adjustedName = new JLabel("Connecticut Avenue");
			adjustedName.setForeground(Color.getHSBColor((float)0.6,(float)1,(float)1));
		}
		else if (str == "StCharlesPlace"){
			adjustedName = new JLabel("St.Charles Place");
			adjustedName.setForeground(Color.magenta);
		}
		else if (str == "StatesAve"){
			adjustedName = new JLabel("States Avenue");
			adjustedName.setForeground(Color.magenta);
		}
		else if (str == "VirginiaAve"){
			adjustedName = new JLabel("Virginia Avenue");
			adjustedName.setForeground(Color.magenta);
		}
		else if (str == "StJamesPlace"){
			adjustedName = new JLabel("St.James Place");
			adjustedName.setForeground(Color.orange);
		}
		else if (str == "TennesseeAve"){
			adjustedName = new JLabel("Tennessee Avenue");
			adjustedName.setForeground(Color.orange);
		}
		else if (str == "NewYorkAve"){
			adjustedName = new JLabel("NewYork Avenue");
			adjustedName.setForeground(Color.orange);
		}
		else if(str == "KentuckyAve"){
			adjustedName = new JLabel("Kentucky Avenue");
			adjustedName.setForeground(Color.red);
		}
		else if(str == "IndianaAve"){
			adjustedName = new JLabel("Indiana Avenue");
			adjustedName.setForeground(Color.red);
		}
		else if(str == "IllinoisAve"){
			adjustedName = new JLabel("Illinois Avenue");
			adjustedName.setForeground(Color.red);
		}
		else if(str == "AtlanticAve"){
			adjustedName = new JLabel("Atlantic Avenue");
			adjustedName.setForeground(Color.yellow);
		}
		else if(str == "VentnorAve"){
			adjustedName = new JLabel("Ventnor Avenue");
			adjustedName.setForeground(Color.yellow);
		}
		else if(str == "MarvinGardens"){
			adjustedName = new JLabel("Marvin Gardens");
			adjustedName.setForeground(Color.yellow);
		}
		else if(str == "PacificAve"){
			adjustedName = new JLabel("Pacific Avenue");
			adjustedName.setForeground(Color.green);
		}
		else if(str == "NorthCarolinaAve"){
			adjustedName = new JLabel("NorthCarolina Avenue");
			adjustedName.setForeground(Color.green);
		}
		else if(str == "PennsylvaniaAve"){
			adjustedName = new JLabel("Pennsylvania Avenue");
			adjustedName.setForeground(Color.green);
		}
		else if(str == "ParkPlace"){
			adjustedName = new JLabel("Park Place");
			adjustedName.setForeground(Color.blue);
		}
		else if(str == "Boardwalk"){
			adjustedName = new JLabel("Boardwalk");
			adjustedName.setForeground(Color.blue);
		}
		else if(str == "WaterWorks"){
			adjustedName = new JLabel("Water Works");
			adjustedName.setForeground(Color.darkGray);
		}
		else if(str == "ElectricCompany"){
			adjustedName = new JLabel("Electric Company");
			adjustedName.setForeground(Color.darkGray);
		}
		else if(str == "ReadingRailroad"){
			adjustedName = new JLabel("Reading Railroad");
			adjustedName.setForeground(Color.black);
		}
		else if(str == "B&ORailroad"){
			adjustedName = new JLabel("B&O Railroad");
			adjustedName.setForeground(Color.black);
		}
		else if(str == "ShortLineRailroad"){
			adjustedName = new JLabel("ShortLine Railroad");
			adjustedName.setForeground(Color.black);
		}
		else if(str == "PennsylvaniaRailroad"){
			adjustedName = new JLabel("Pennsylvania Railroad");
			adjustedName.setForeground(Color.black);
		}
		
		return adjustedName;
	}
	
}